//修改滑块相关操作
function changeSliderWidth(isProgress, width) {
  $("#sliderProgress").css("width", width + 13);

  $("#sliderBall").css("left", width);

  $("#sliderText").css("left", width);

  var time = parseInt(
    ((width + 13) * totalDuration) / $("#sliderBackground").width()
  );

  $("#sliderText").html(getTimeEmt(time));

  if (!isProgress && enableDrag) {
    currentime = time;

    changeCurrentPlay(time);
  }
}

function adjustY(y) {
  var newY = ((100 - (y * 4) / 3) / 100).toFixed(2);

  return newY;
}

$(function () {
  //滑块拖动
  $(document)
    .mousemove(function (e) {
      if (!!this.move) {
        sliderInProgress = true;

        var realX = document.move_target.realX;

        var posix = !document.move_target ? 0 : document.move_target.posix,
          callback =
            document.call_down ||
            function () {
              var xPos = realX + e.pageX - posix;

              if (xPos < 0) xPos = 0;
              else if (xPos > $("#sliderBackground").width())
                xPos = $("#sliderBackground").width();

              $("#sliderText").show();

              $(this.move_target).css({
                left: xPos,
              });

              changeSliderWidth(sliderInProgress, xPos);
            };

        callback.call(this, e, posix);
      }
    })
    .mouseup(function (e) {
      if (!!this.move) {
        sliderInProgress = false;

        var realX = document.move_target.realX;

        var posix = !document.move_target ? 0 : document.move_target.posix,
          callback =
            document.call_up ||
            function () {
              var left = realX + e.pageX - posix;

              $("#sliderText").hide();

              changeSliderWidth(sliderInProgress, left);
            };
        callback.call(this, e, posix);
        $.extend(this, {
          move: false,
          move_target: null,
          call_down: false,
          call_up: false,
        });
      } else {
        $("#soundBall").off("mousemove");
      }
    });

  $("#sliderBall").mousedown(function (e) {
    e.preventDefault();

    var offset = $(this).offset();

    var realX = $(this).position().left;

    this.posix = e.pageX;

    this.realX = $(this).position().left;

    $.extend(document, { move: true, move_target: this });
  });

  $("#sliderBackground").click(function (e) {
    if (!sliderInProgress) {
      var posX = $("#sliderBackground").offset().left;

      var position = e.pageX - posX;

      changeSliderWidth(false, parseInt(position - 13));
    }
  });

  $("#soundBackground").mousedown(function (e) {
    var oldY = e.pageY;

    var oldPositionY = $("#soundBall").position().top;

    if (e.target.id == "soundBall") {
      $("#soundBall").mousemove(function (e) {
        var newY = e.pageY;

        var moveY = newY - oldY;

        var aimY = oldPositionY + moveY;

        if (aimY <= 0) aimY = 0;
        else if (aimY > $("#soundBallProgress").height())
          aimY = $("#soundBallProgress").height();

        $("#soundBall").css("top", aimY);

        changeSound(adjustY(aimY));

        $("#soundBall").mouseup(function (e) {
          changeSound(adjustY(aimY));

          $("#soundBall").off("mousemove");
        });
      });
    }
  });

  $("#soundPanel")
    .mouseover(function () {
      $("#soundBackground").show();

      $("#soundPanel").css("zIndex", 999);
    })
    .mouseleave(function () {
      $("#soundBackground").hide();

      $("#soundPanel").css("zIndex", 2);
    });
});
